package dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

import beans.VulnerabilityMaster;

public class VulnerabiltyDAOImpl implements VulnerabilityDAO {

	private SessionFactory sessionFactory;
	public static final String TABLE = "TEST_VULN_MASTER";

	public void setSessionFactory(SessionFactory sf) {
		this.sessionFactory = sf;
	}

	@Override
	public VulnerabilityMaster addVulnerability(VulnerabilityMaster p) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session session = sf.openSession();

		session.beginTransaction();

		session.save(p);
		
		session.getTransaction().commit();
		session.close();
		sf.close();

		return p;

	}
	@Override
	public List<VulnerabilityMaster> getAllVulnerabilities() {
		// TODO Auto-generated method stub
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session session = sf.openSession();
		  session.beginTransaction();  


		Query q = session.createQuery("from VulnerabilityMaster");

		List<VulnerabilityMaster> list = q.list();

		//System.out.println("records size is" + list.size());
		  session.getTransaction().commit();  

		return list;

	}

	/*
	 * @Override public void updatePerson(Person p) { Session session =
	 * this.sessionFactory.getCurrentSession(); session.update(p);
	 * System.out.println("Person updated successfully, Person Details="+p); }
	 * 
	 * @SuppressWarnings("unchecked")
	 * 
	 * @Override public List<Person> listPersons() { Session session =
	 * this.sessionFactory.getCurrentSession(); List<Person> personsList =
	 * session.createQuery("from Person").list(); for(Person p : personsList){
	 * System.out.println("Person List::"+p); } return personsList; }
	 */

}